package com.jandan.ui.client;

import java.util.Iterator;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.HTMLPanel;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.jandan.ui.client.service.WordLibService;
import com.jandan.ui.client.service.WordLibServiceAsync;
import com.jandan.ui.model.WordLib;

public class DisplayAllLibPanel extends Panel {
	private ILessonOperator lessonOperator;

	public DisplayAllLibPanel(ILessonOperator lessonOperator) {
		this.lessonOperator = lessonOperator;
		this.setTitle("所有词库");
		this.setLayout(new VerticalLayout(20));
		this.setAutoScroll(true);
		createComponent();
	}
	
	public DisplayAllLibPanel(List<WordLib> wordLibList){
		this.setTitle("所有词库");
		this.setLayout(new VerticalLayout(20));
		this.setAutoScroll(true);
		if (wordLibList != null && wordLibList.size() > 0) {
			Iterator<WordLib> it = wordLibList.iterator();

			while (it.hasNext()) {
				WordLib wordLib = it.next();
				LibPanel libPanel = new LibPanel(wordLib);
				addLibPanel(libPanel);
			}
		}
	}
	

	private void createComponent() {
		// 获取所有的词库List
		WordLibServiceAsync wordLibService = WordLibService.Util.getInstance();
		wordLibService.getWordLibList(new AsyncCallback<List<WordLib>>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				MessageBox.alert("获取词库信息错误");
			}

			public void onSuccess(List<WordLib> wordLibList) {
				// TODO Auto-generated method stub
			
				if (wordLibList != null && wordLibList.size() > 0) {
					MessageBox.alert("shit");
					Iterator<WordLib> it = wordLibList.iterator();

					while (it.hasNext()) {
						WordLib wordLib = it.next();
						LibPanel libPanel = new LibPanel(wordLib);
						addLibPanel(libPanel);
					}
				}
			}
		});
	}

	private void addLibPanel(LibPanel libPanel) {
		this.add(libPanel);
	}

	private class LibPanel extends Panel {
		WordLib wordLib;

		public LibPanel(WordLib wordLib) {
			// this.setLayout(new Cloum);
			this.wordLib = wordLib;
			this.setLayout(new HorizontalLayout(20));
			
			createComponent();
		}

		private void createComponent() {
			// 1图片
			Panel imagePanel = new Panel();
			imagePanel.setHeight(120);
			imagePanel.setWidth(64);
			imagePanel.setLayout(new FitLayout());
			Image img = new Image(wordLib.getLibImagePath());
			img.setPixelSize(64, 90);
			imagePanel.add(img);

			this.add(imagePanel);

			// 2词库信息
			// Panel libInfoPanel = new Panel();
			Panel libInfoPanel = new Panel();
			libInfoPanel.setLayout(new VerticalLayout(20));

			Label id = new Label("" + wordLib.getWordLibID());
        			id.setVisible(false);
			libInfoPanel.add(id);

			StringBuilder libInfo = new StringBuilder();

			libInfo.append("<table class='wordlib-info'>");
			libInfo.append("<tr><td>");
			libInfo.append("词库名:");
			libInfo.append("</td><td>");
			libInfo.append(wordLib.getWordLibName());
			libInfo.append("</td></tr>");
			libInfo.append("<tr><td>");
			libInfo.append("描述:");
			libInfo.append("</td><td>");
			libInfo.append(wordLib.getDescription());
			libInfo.append("</td></tr>");
			libInfo.append("<tr><td>");
			libInfo.append("单词数:");
			libInfo.append("</td><td>");
			libInfo.append(wordLib.getTotalWordCount());
			libInfo.append("</td></tr>");
			libInfo.append("<tr><td>");
			libInfo.append("学习人数:");
			libInfo.append("</td><td>");
			libInfo.append(wordLib.getTotalStudyPersonCount());
			libInfo.append("</td></tr>");
			libInfo.append("</table>");

			HTMLPanel info = new HTMLPanel(libInfo.toString());

			libInfoPanel.add(info);

			Hyperlink link = new Hyperlink("",true,"");
			link.setText("浏览词库");
			link.addStyleName("wordlib-link");

			link.addClickListener(new ClickListener() {

				public void onClick(Widget w) {
					// TODO Auto-generated method stub
					lessonOperator.checkLib(wordLib);
				}
			});
			libInfoPanel.add(link);
			this.add(libInfoPanel);

		}
	}

	public void setLessonOperator(ILessonOperator lessonOperator) {
		// TODO Auto-generated method stub
		this.lessonOperator = lessonOperator;
	}
}
